package com.xbb.monitor.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

@Service
public class MonitorService {
    @Autowired
    private CpuService cpuService;
    @Autowired
    private DiskService diskService;
    @Autowired
    private NetworkService networkService;


    public Map<String,Object> diskAjax(String mont, String timeFlag){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String endTime = sdf.format(calendar.getTime());
        calendar = cpuService.calculateTime(calendar,timeFlag);
        int interval = cpuService.getInterval(timeFlag);
        if(interval == 0){
            return null;
        }
        String startTime = sdf.format(calendar.getTime());

        Map<String,Object> map = diskService.getDataByPath(calendar,interval,sdf,mont);
        map.put("startTime",startTime);
        map.put("endTime",endTime);

        return map;
    }

    public Map<String,Object> networkAjax(String networkName, String timeFlag){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String endTime = sdf.format(calendar.getTime());
        calendar = cpuService.calculateTime(calendar,timeFlag);
        int interval = cpuService.getInterval(timeFlag);
        if(interval == 0){
            return null;
        }
        String startTime = sdf.format(calendar.getTime());

        Map<String,Object> map = networkService.getData(calendar,interval,sdf,networkName);
        map.put("startTime",startTime);
        map.put("endTime",endTime);

        return map;
    }
}
